package cn.com.south.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;

/**
 * @author south
 * date 2021/12/30 22:20
 * @version v1.0.0
 */
public class MD5Utils {
    private static final char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public MD5Utils() {
    }

    public static String encode(File file) {
        FileInputStream in = null;
        MessageDigest md5 = null;

        try {
            in = new FileInputStream(file);
            FileChannel ch = in.getChannel();
            MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0L, file.length());
            md5 = MessageDigest.getInstance("MD5");
            md5.update(byteBuffer);
        } catch (Exception var13) {
            var13.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException var12) {
                var12.printStackTrace();
            }

        }

        return toHex(md5.digest());
    }

    public static String getMD5(File file) {
        FileInputStream fis = null;

        Object var3;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            fis = new FileInputStream(file);
            byte[] buffer = new byte[2048];
            boolean var4 = true;

            int length;
            while((length = fis.read(buffer)) != -1) {
                md.update(buffer, 0, length);
            }

            byte[] b = md.digest();
            String var6 = byteToHexString(b);
            return var6;
        } catch (Exception var16) {
            var16.printStackTrace();
            var3 = null;
        } finally {
            try {
                fis.close();
            } catch (IOException var15) {
                var15.printStackTrace();
            }

        }

        return (String)var3;
    }

    private static String byteToHexString(byte[] tmp) {
        char[] str = new char[32];
        int k = 0;

        for(int i = 0; i < 16; ++i) {
            byte byte0 = tmp[i];
            str[k++] = hexDigits[byte0 >>> 4 & 15];
            str[k++] = hexDigits[byte0 & 15];
        }

        String s = new String(str);
        return s;
    }

    public static void main(String[] arg) {
        String admin = encode("admin");
        System.out.println(admin);
    }

    public static String encode(String arg) {
        if (arg == null) {
            arg = "";
        }

        MessageDigest md5 = null;

        try {
            md5 = MessageDigest.getInstance("MD5");
            md5.update(arg.getBytes("UTF-8"));
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return toHex(md5.digest());
    }

    private static String toHex(byte[] bytes) {
        StringBuffer str = new StringBuffer(32);
        byte[] var2 = bytes;
        int var3 = bytes.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            byte b = var2[var4];
            str.append(hexDigits[(b & 240) >> 4]);
            str.append(hexDigits[b & 15]);
        }

        return str.toString();
    }

}
